<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="88px">
      <el-form-item label="用户名" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="resetQuery">重置</el-button>
        <el-button type="primary" @click="handleQuery">查询</el-button>
      </el-form-item>
    </el-form>
    <el-form size="small">
      <el-form-item>
        <el-button type="primary" @click="add">新增</el-button>
      </el-form-item>
    </el-form>
    <el-radio-group v-model="tabPosition" style="margin-bottom: 20px;">
      <el-radio-button label="0">全部</el-radio-button>
      <el-radio-button label="1">代理商</el-radio-button>
      <el-radio-button label="2">门店</el-radio-button>
    </el-radio-group>
    <el-table
      v-loading="loading"
      :data="list"
      style="width: 100%;"
    >
      <el-table-column label="序号" type="index">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户名" prop="nickName" />
      <el-table-column label="手机号" prop="phonenumber" />
      <el-table-column label="用户类型">
        <template slot-scope="scope">
          <span v-show="scope.row.userType == '01'">代理商</span>
          <span v-show="scope.row.userType == '04'">门店</span>
        </template>
      </el-table-column>
      <el-table-column label="用户余额" prop="balance" >
        <template slot-scope="scope">
          {{scope.row.balance ? scope.row.balance.toFixed(2) : '0.00'}}
        </template>
      </el-table-column>
      <el-table-column label="绑定终端(代理商)数量" prop="terminalCount" />
      <el-table-column label="收费比例" >
        <template slot-scope="scope">
          {{scope.row.discount + '%'}}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" style="color:#386BD7" @click="edit(scope.row)">编辑</el-button>
          <el-button type="text" style="color:#FF5555" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    <!-- 添加 修改终端 -->
    <el-dialog :title="title" :visible.sync="open" width="30%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phonenumber">
          <el-input v-model="form.phonenumber" @input="val=>form.phonenumber=val.replace(/[^\d]/g,'')" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="用户类型" prop="userType">
          <el-select v-model="form.userType" :disabled="form.userId ? true : false" clearable style="width:100%">
            <el-option
              v-for="item in userTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="费用比例" prop="discount">
          <el-input v-model="form.discount" @input="val=>form.discount=val.replace(/[^\d]/g,'')" placeholder="请输入">
            <span slot="suffix">%</span>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
    title="提示"
    :visible.sync="delVisible"
    width="30%">
    <span>是否删除？</span>
    <span slot="footer" class="dialog-footer">
      <el-button @click="delVisible = false">取 消</el-button>
      <el-button type="primary" @click="delConfirm">确 定</el-button>
    </span>
  </el-dialog>
  </div>
</template>

<script>
import { getAgentList,addAgent,updateAgent,delPackage } from "@/api/common";

export default {
  name: "Online",
  data() {
    return {
      tabPosition:'0',
      delVisible:false,
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userType:null
      },
      statusList:[
        {value:'1',label:'激活'},
        {value:'0',label:'未激活'},
      ],
      userTypeList:[
        {value:'01',label:'代理商'},
        {value:'04',label:'门店'}
      ],
      form:{},
      rules: {
        nickName: [
          { required: true, message: "用户名称不能为空", trigger: "blur" }
        ],
        phonenumber: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        userType: [
          { required: true, message: "用户类型不能为空", trigger: "blur" }
        ],
        discount:[
          { required: true, message: "费用比例不能为空", trigger: "blur" }
        ],
      },
      title:'',
      open:false
    };
  },
  created() {
    this.getList();
  },
  watch:{
    tabPosition(val){
      if(val == '0'){
        this.queryParams.userType = null
      }
      if(val == '1'){
        this.queryParams.userType = '01'
      }
      if(val == '2'){
        this.queryParams.userType = '04'
      }
      this.getList()
    }
  },
  methods: {
    getList() {
      this.loading = true;
      getAgentList(this.queryParams).then(response => {
        this.list = response.rows
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams =  {
        pageNum: 1,
        pageSize: 10
      }
      this.handleQuery();
    },
    add(){
      this.form = {}
      this.open = true;
      this.title = "新增代理商";
    },
    edit(row){
      this.form = row
      this.open = true;
      this.title = "编辑代理商";
    },
    del(row){
      this.form = row
      this.delVisible = true
    },
    delConfirm(){
      this.delVisible = false
      delPackage(this.form.id).then(res=>{
        if(res.code == 200){
          this.$message.success('删除成功')
          this.getList()
        }
      })
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.discount = this.form.discount/100
          if (this.form.userId != undefined) {
            updateAgent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.userName = this.form.phonenumber
            addAgent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.warnStatus{
  display: flex;
  justify-content: center;
}
.circle{
  width: 8px;
  height: 8px;
  border-radius: 50%;
}
.terminalContent{
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 40px;
  .terminalItem{
    width: 50%;
  }
}
.dealContent{
  display: flex;
  align-items: center;
  height: 40px;
}
</style>

